<template>
    <el-form :model="order" label-position="right" label-width="auto">
        <el-row :gutter="10">
            <el-col :span="12">
                <el-form-item label="订单号" prop="dingdan_num">
                    <el-input v-model="order.dingdan_num" placeholder="订单号" disabled/>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="订单状态" prop="state">
                    <el-select v-model="order.state" placeholder="选择状态">
                        <el-option value="0">进行中</el-option>
                        <el-option value="1">已完成</el-option>
                        <el-option value="2">已取消</el-option>
                    </el-select>
                </el-form-item>
            </el-col>
        </el-row>

        <el-row :gutter="10">
            <el-col :span="12">
                <el-form-item label="骑手ID" prop="rider_id">
                    <el-input v-model="order.rider_id" placeholder="骑手ID" clearable/>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="订单金额" prop="money">
                    <el-input v-model="order.money" placeholder="订单金额" type="number" clearable/>
                </el-form-item>
            </el-col>
        </el-row>

        <el-row :gutter="10">
            <el-col :span="12">
                <el-form-item label="小费" prop="gratuity">
                    <el-input v-model="order.gratuity" placeholder="小费金额" type="number" clearable/>
                </el-form-item>
            </el-col>
            <el-col :span="12">
                <el-form-item label="完成时间" prop="end_time">
                    <el-date-picker v-model="order.end_time" type="datetime" placeholder="选择完成时间"/>
                </el-form-item>
            </el-col>
        </el-row>

        <el-form-item label="备注" prop="remark">
            <el-input v-model="order.remark" placeholder="备注信息" type="textarea" rows="3"/>
        </el-form-item>

        <el-row>
            <el-col>
                <div style="text-align: center;">
                    <el-button type="primary" @click="doSave">保存</el-button>
                    <el-button @click="doCancel">取消</el-button>
                </div>
            </el-col>
        </el-row>
    </el-form>
</template>

<script setup lang="ts">
import {ref} from 'vue';
import {ElMessage} from "element-plus";
import axios from 'axios';

// 自定义组件事件
const emits = defineEmits(["closeDialog", "flushPage"]);

const order = ref({
    id: '',
    dingdan_num: '',
    rider_id: '',
    state: '',
    money: 0,
    gratuity: 0,
    end_time: '',
    remark: ''
});

// 接收父组件传递的订单数据
const handleUpdateData = (obj) => {
    order.value = {...obj};
};

// 暴露方法给父组件
defineExpose({
    handleUpdateData
});

// 取消按钮
const doCancel = () => {
    emits("closeDialog");
};

// 保存更新
const doSave = async () => {
    try {
        const { data } = await axios.put('/api/dingdan', order.value);
        if (data > 0) {
            emits("flushPage");
            emits("closeDialog");
            ElMessage.success("更新订单成功");
        } else {
            ElMessage.error("更新订单失败");
        }
    } catch (error) {
        ElMessage.error("更新订单失败");
        console.error(error);
    }
};
</script>

<style scoped>
</style>